package com.jxb.test20241216;

import com.jxb.tree.TreeNode;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/12/22 23:49
 **/
public class BalancedBinTree_110 {

    public static void main(String[] args) {

    }

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return depth(root) != -1;
    }

    private int depth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = depth(root.left);
        int right = depth(root.right);
        if (left == -1 || right == -1 || Math.abs(left - right) > 1) {
            return -1;
        }
        return Math.max(left,right) + 1;
    }

}
